package com.zy.web.demo.duoxiancheng.demo9;

/**
 * @author <a href="mailto:zhouyong@cqyyt.com">zy</a>
 * @version 1.0
 * @date 2020/7/29 14:21
 * a线程调用带synchronized的方法，b调用带synchronized的方法，a先执行，b线程等a执行完才可以执行
 */
public class Demo9 {

    private int num;

    synchronized public void addId(String name){
        try {
            if (name.equals("a")){
                num = 100;
                Thread.sleep(1000);
                System.out.println("a set over");
            }else {
                num = 200;
                System.out.println("b set over");
            }
            System.out.println(name+" num=" +num);

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public void addId2(String name){
        try {
            if (name.equals("a")){
                num = 100;
                Thread.sleep(1000);
                System.out.println("a set over");
            }else {
                num = 200;
                System.out.println("b set over");
            }
            System.out.println(name+" num=" +num);

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

}
